package edu.xcdq;

import com.mysql.jdbc.Driver;

import java.sql.*;

public class Demo01 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {

            // 1 注册驱动
            //DriverManager.registerDriver(new Driver());
            Class.forName("com.mysql.jdbc.Driver");
            // 2 获取链接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
            // 3 准备状态
            statement = connection.createStatement();
            // 4 准备sql执行
            resultSet = statement.executeQuery("select * from book");
            // 5 获取结果并遍历结果集
            while (resultSet.next()) {
                System.out.println(resultSet.getInt(1) + resultSet.getString(2)
                        + resultSet.getString(3) + resultSet.getDouble(4)
                );
            }

        } catch (Exception e) {
            System.out.println("可能获取驱动有问题");
        } finally {
            // 6 关闭资源
            try {
                resultSet.close();
                //System.out.println("申请资源不成功，无法关闭");
                System.out.println("网络开小差了哦，亲");
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }
}